// Chart.cpp: implementation of the CChart class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "StructSnapshot.h"
#include "Chart.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CChart::CChart()
:m_nlevel(0)
{

}

CChart::~CChart()
{
	cleanlevels();
}

void CChart::addlevel(UINT nLevel)
{
	for (UINT i=0; i<nLevel; i++)
	{
		m_arrLevel.Add(new chLevel);
		m_arrLevel[i]->level = m_nlevel+i;
		m_arrLevel[i]->total = 0;
	}
	m_nlevel += nLevel;
}

void CChart::addlevel(chLevel& level)
{
	m_nlevel++;
	m_arrLevel.Add(&level);
}
void CChart::removelevel(UINT nlevel)
{
	ASSERT((nlevel>0)&&(nlevel<(UINT)m_arrLevel.GetSize()));
	m_arrLevel.RemoveAt(nlevel);
}

chLevel* CChart::getlevel(UINT nlevel)
{
	ASSERT((nlevel>0)&&(nlevel<(UINT)m_arrLevel.GetSize()));
	return m_arrLevel[nlevel];
}

void CChart::cleanlevels()
{
	int nSize = m_arrLevel.GetSize();
	for(int i=0; i<nSize; i++)
	{
		if (m_arrLevel[i] == NULL)
		{
			continue;
		}
		cleanitems(m_arrLevel[i]);
		delete m_arrLevel[i];
		m_arrLevel[i] = NULL;
	}
	m_arrLevel.RemoveAll();
}

void CChart::additem(UINT nLevel, UINT nsize)
{
	m_arrLevel[nLevel]->arrItem.Add(new chItem);
	int size = m_arrLevel[nLevel]->arrItem.GetSize();
	m_arrLevel[nLevel]->arrItem[size-1]->level = nLevel;
	m_arrLevel[nLevel]->arrItem[size-1]->size = nsize;
	m_arrLevel[nLevel]->arrItem[size-1]->pContent = NULL;

	//for(UINT i=0; i<nItem; i++)
	//{
	//	m_arrLevel[nLevel]->arrItem.Add(new chItem);
	//	m_arrLevel[nLevel]->arrItem[nsize+i]->level = nLevel;
	//	m_arrLevel[nLevel]->arrItem[nsize+i]->size = 0;
	//	m_arrLevel[nLevel]->arrItem[nsize+i]->pContent = NULL;
	//}
}

void CChart::removeitem(UINT nLevel, UINT nItem)
{
	ASSERT((nLevel>0)&&(nLevel<(UINT)m_arrLevel.GetSize()));
	ASSERT((nItem>0)&&(nItem<(UINT)m_arrLevel[nLevel]->arrItem.GetSize()));

}

chItem* CChart::getitem(UINT nLevel, UINT nItem)
{
	ASSERT((nLevel>0)&&(nLevel<(UINT)m_arrLevel.GetSize()));
	ASSERT((nItem>0)&&(nItem<(UINT)m_arrLevel[nLevel]->arrItem.GetSize()));
	return m_arrLevel[nLevel]->arrItem[nItem];
}

void CChart::cleanitems(chLevel* level)
{
	int nSize = level->arrItem.GetSize();
	for (int i=0; i<nSize; i++)
	{
		if (level->arrItem[i] == NULL)
		{
			continue;
		}
		delete (level->arrItem[i]);
		level->arrItem[i] = NULL;
	}
	level->arrItem.RemoveAll();
}

void CChart::CountLevelSize()
{
	int levelSize = m_arrLevel.GetSize();
	m_arrLevel[0]->used = 0;
	for (int i=1; i<levelSize; i++)
	{
		int itemSize = m_arrLevel[i]->arrItem.GetSize();
		UINT us = m_arrLevel[i-1]->used;
		UINT to = m_arrLevel[i-1]->total;
		UINT tl = m_arrLevel[i]->total;
		UINT tot = (1.0f-((FLOAT)(m_arrLevel[i-1]->used)/(FLOAT)(m_arrLevel[i-1]->total)))*m_arrLevel[i]->total;
		m_arrLevel[i]->total += tot;
		m_arrLevel[i]->used = 0;
		for(int j=0; j<itemSize; j++)
		{
			m_arrLevel[i]->used += m_arrLevel[i]->arrItem[j]->size;
		}
	}
}